var app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:'',
    title:'',
    commit: 33,
    date:'',
    read:'',
    style: 'font-size:15px; color:#313131; font-weight:normal;',
    commitsArray:[],
    html: '',
    adverTitle:'',
    adverImg:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that=this
    console.log(options)
   
   var id=options.id
    console.log(id)
   this.setData({
     id:id
   })
  
  },
  submit:function(event){
    var that=this
    console.log(event)
    // console.log(this.data.id)
    var value=event.detail.v
    if(value==''){
      wx.showToast({
        title: '评论内容不能为空~~',
        icon:'none'
      })
    }else{
      
    console.log(value)
    wx.request({
      url: app.globalData.baseUrl+'news/comment',
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      data:{
        newsId:that.data.id,
        content:value
      },
      method:'POST',
     
      success:function(res){
        console.log(res)
        that.onShow()
      }
    })

    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that=this
    wx.request({
      url: app.globalData.baseUrl + 'news/id',
      method: 'GET',
      data: {
        newsId:that.data.id
      },
      header: {
        'cookie': 'JSESSIONID=' + wx.getStorageSync('sessionID')
      },
      success: function (res) {
        console.log(res)
        var data = res.data.data.data
        var adver=res.data.data.advertisement
        //  console.log(data.read)
        var time = data.gmtCreateFormat
         time=time.substring(0,10)
        that.setData({
          html: data.content,
          title: data.title,
           cover_img:data.cover_img,
           date:time,
           adverTitle:adver.name,
           adverImg:adver.url,
          read: data.read + 1,
          commit: data.commentTotal
        })
        for (var i = 0; i < data.commentList.length; i++) {
          var avatar = 'commitsArray[' + i + '].avatar'
          var nickName = 'commitsArray[' + i + '].nickName'
          var date = 'commitsArray[' + i + '].date'
          var content = 'commitsArray[' + i + '].content'
          that.setData({
            [avatar]: data.commentList[i].user.avatarUrl,
            [nickName]: data.commentList[i].user.nickName,
            [date]: data.commentList[i].gmtCreateFormat.substring(0, 10),
            [content]: data.commentList[i].content

          })
        }
      }
    })

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})